package upload;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class RecursiveDirReader {
	private List<File> allFiles;
	
	public RecursiveDirReader() {
		allFiles = new ArrayList<File>();
	}

	private boolean isValidFormat(String fileName) {
		String format = fileName.substring(fileName.length() - 3);
		if(format.equalsIgnoreCase("jpg")) {
			return true;
		} else if(format.equalsIgnoreCase("png")) {
			return true;
		} else if(format.equalsIgnoreCase("gif")) {
			return true;
		}else {
			return false;
		}
	}
	
	private List<File> getAllFiles(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			for (int i = 0; i < children.length; i++) {
				getAllFiles(new File(dir, children[i]));
			}
		} else {
			if(isValidFormat(dir.getName())){
				this.allFiles.add(dir);
			}
		}
		return this.allFiles;
	}
}
